/*
package org.nmgyjt.springboot.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nmgyjt.springboot.utils.NoSensitiveUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Slf4j
@Component
public class LogInterceptor implements Filter {


    */
/**
     * 过滤不需要的路径
     *//*

    private String[] passUrl = {
    };

    */
/**
     * 判断本次请求的数据类型是否为json
     *
     * @param request request
     * @return true: 是 JSON 数据; false: 非 json 数据
     *//*

    private boolean isJson(HttpServletRequest request) {
        if (request.getContentType() != null) {
            return request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE) ||
                    request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE);
        }

        return false;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //接口开始调用时间
        long startTime = System.currentTimeMillis();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String url = ((HttpServletRequest) servletRequest).getRequestURL().toString();
        if (StringUtils.endsWith(url, "/prometheus")) {
            //不打印Prometheus日志
            filterChain.doFilter(request, servletResponse);
            return;
        }
        String requestURI = ((HttpServletRequest) servletRequest).getRequestURI();
        if (Arrays.asList(passUrl).contains(url) || requestURI.equals("/")) {
            filterChain.doFilter(request, servletResponse);
            return;
        }

        RequestWrapper requestWrapper = new RequestWrapper(request);
        BodyCachingHttpServletResponseWrapper responseWrapper =
                new BodyCachingHttpServletResponseWrapper((HttpServletResponse) servletResponse);
        logDTOAndResponse(requestWrapper, url, responseWrapper, "request", startTime);
        filterChain.doFilter(request, responseWrapper);
        logDTOAndResponse(requestWrapper, url, responseWrapper, "response", startTime);
    }

    void logDTOAndResponse(RequestWrapper requestWrapper, String url, BodyCachingHttpServletResponseWrapper responseWrapper, String type, long startTime) throws IOException {
        if (type.equals("request")) {
            Enumeration headerNames = requestWrapper.getHeaderNames();
            //使用循环遍历请求头，并通过getHeader()方法获取一个指定名称的头字段
            List<Map> headers = new ArrayList<>();
            while (headerNames.hasMoreElements()) {
                Map headMap = new HashMap<String, String>();
                String headerName = (String) headerNames.nextElement();
                String headerValue = requestWrapper.getHeader(headerName);
                headMap.put(headerName, headerValue);
                headers.add(headMap);
            }
            JSONObject obj = null;
            if (StringUtils.isNotBlank(requestWrapper.getBody())) {
                obj = JSON.parseObject(requestWrapper.getBody());
                desensitization(obj);
            }
            log.info("【请求时间】:{},【请求url】:{},【请求方式】:{},【headers】{},【请求体】:{}", new Date(), url, requestWrapper.getMethod(), headers, obj);
        } else {
            byte[] responseBody = responseWrapper.getBody();
            String responseBodyStr = new String(responseBody);
            long endTime;
            JSONObject obj = JSON.parseObject(responseBodyStr);
            desensitization(obj);
            endTime = System.currentTimeMillis();
            log.info("【响应时间】:{},【url】:{},【响应体】:{},【处理时间】:{}", new Date(), url, obj, (endTime - startTime) + "ms");
        }
    }

    */
/**
     * 身份证手机号脱敏
     *
     * @param obj
     *//*

    private void desensitization(JSONObject obj) {
        if (obj.containsKey("idNo")) {
            //身份证号码脱敏
            String idNo = NoSensitiveUtils.desIdCardForString(obj.getString("idNo"));
            obj.put("idNo", idNo);
        }
        if (obj.containsKey("mobile")) {
            String mobile = NoSensitiveUtils.desMobileForString(obj.getString("mobile"));
            obj.put("mobile", mobile);
        }
    }
}*/
